package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.MesProduceOrderRpcService;
import com.hunttown.mes.rpc.domain.MesProduceOrderDTO;
import com.hunttown.mes.rpc.domain.query.MesProduceOrderDTOQuery;
import com.hunttown.mes.service.MesProduceOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static com.hunttown.mes.convert.MesProduceOrderConverts.convert;

/**
 * created by wangjunfu on 2022-03-30 15:29:00
 */
@Service
public class MesProduceOrderRpcServiceImpl implements MesProduceOrderRpcService {

    private final Logger logger = LoggerFactory.getLogger(MesProduceOrderRpcServiceImpl.class);

    private final MesProduceOrderService mesProduceOrderService;

    @Autowired
    public MesProduceOrderRpcServiceImpl(MesProduceOrderService mesProduceOrderService) {
        this.mesProduceOrderService = mesProduceOrderService;
    }

    @Override
    public MesProduceOrderDTO insert(MesProduceOrderDTO mesProduceOrderDTO) {
        try {
            return convert(mesProduceOrderService.insert(convert(mesProduceOrderDTO)));
        } catch (Exception e) {
            logger.error("MesProduceOrder create failed.", e);
            throw new RuntimeException("MesProduceOrder create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(MesProduceOrderDTO mesProduceOrderDTO) {
        try {
            return mesProduceOrderService.updateInfoById(convert(mesProduceOrderDTO));
        } catch (Exception e) {
            logger.error("MesProduceOrder update by id failed.", e);
            throw new RuntimeException("MesProduceOrder update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(MesProduceOrderDTOQuery mesProduceOrderDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesProduceOrderDTOQuery.getW_id() != null && mesProduceOrderDTOQuery.getW_id() > 0) {
                query.put("w_id", mesProduceOrderDTOQuery.getW_id());
            }

            if (mesProduceOrderDTOQuery.getIds() != null && !mesProduceOrderDTOQuery.getIds().equals("")) {
                query.put("ids", mesProduceOrderDTOQuery.getIds());
            }

            if (mesProduceOrderDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesProduceOrderDTOQuery.getStopFlag());
            }

            if (mesProduceOrderDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesProduceOrderDTOQuery.getDeleteFlag());
            }

            return mesProduceOrderService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("MesProduceOrder update by query failed.", e);
            throw new RuntimeException("MesProduceOrder update by query failed, Please order your code.", e);
        }
    }

    @Override
    public MesProduceOrderDTO selectById(Integer id) {
        try {
            return convert(mesProduceOrderService.selectById(id));
        } catch (Exception e) {
            logger.error("MesProduceOrder select by id failed.", e);
            throw new RuntimeException("MesProduceOrder select by id failed, Please order your code.", e);
        }
    }

    @Override
    public MesProduceOrderDTO selectObjByQuery(MesProduceOrderDTOQuery mesProduceOrderDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (StringUtils.isNotBlank(mesProduceOrderDTOQuery.getOrderCode())) {
                query.put("orderCode", mesProduceOrderDTOQuery.getOrderCode());
            }

            if (mesProduceOrderDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesProduceOrderDTOQuery.getStopFlag());
            }

            if (mesProduceOrderDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesProduceOrderDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(mesProduceOrderDTOQuery.getOrderBy())) {
                query.put("orderBy", mesProduceOrderDTOQuery.getOrderBy());
            }

            return convert(mesProduceOrderService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("MesProduceOrder select obj by query failed.", e);
            throw new RuntimeException("MesProduceOrder select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<MesProduceOrderDTO> getListForPage(MesProduceOrderDTOQuery mesProduceOrderDTOQuery) {
        Query query = Query.page(mesProduceOrderDTOQuery.getPn(), mesProduceOrderDTOQuery.getPageSize());

        if (mesProduceOrderDTOQuery.getId() != null && mesProduceOrderDTOQuery.getId() > 0) {
            query.put("id", mesProduceOrderDTOQuery.getId());
        }

        if (mesProduceOrderDTOQuery.getIds() != null && !mesProduceOrderDTOQuery.getIds().equals("")) {
            query.put("ids", mesProduceOrderDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(mesProduceOrderDTOQuery.getLikeName())) {
            query.put("likeName", mesProduceOrderDTOQuery.getLikeName());
        }

        if (mesProduceOrderDTOQuery.getParentId() != null) {
            query.put("parentId", mesProduceOrderDTOQuery.getParentId());
        }

        if (mesProduceOrderDTOQuery.getStatus() != null) {
            query.put("status", mesProduceOrderDTOQuery.getStatus());
        }

        if (mesProduceOrderDTOQuery.getBeforeStatus() != null) {
            query.put("beforeStatus", mesProduceOrderDTOQuery.getBeforeStatus());
        }

        if (mesProduceOrderDTOQuery.getAfterStatus() != null) {
            query.put("afterStatus", mesProduceOrderDTOQuery.getAfterStatus());
        }

        //区间日期
        if (StringUtils.isNotBlank(mesProduceOrderDTOQuery.getDaterange())) {
            String[] date = mesProduceOrderDTOQuery.getDaterange().split("~");
            if (date.length == 2) {
                query.put("startDate", date[0].trim());
                query.put("endDate", date[1].trim());
            }
        }

        if (StringUtils.isNotBlank(mesProduceOrderDTOQuery.getShowDate())) {
            query.put("showDate", mesProduceOrderDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(mesProduceOrderDTOQuery.getStartDate())) {
            query.put("startDate", mesProduceOrderDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(mesProduceOrderDTOQuery.getEndDate())) {
            query.put("endDate", mesProduceOrderDTOQuery.getEndDate());
        }

        if (mesProduceOrderDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", mesProduceOrderDTOQuery.getStopFlag());
        }

        if (mesProduceOrderDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", mesProduceOrderDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(mesProduceOrderDTOQuery.getOrderBy())) {
            query.put("orderBy", mesProduceOrderDTOQuery.getOrderBy());
        }

        List<MesProduceOrderDTO> list = convert(mesProduceOrderService.selectByQuery(query));
        Page<MesProduceOrderDTO> page = Page.from(mesProduceOrderDTOQuery.getPn(), mesProduceOrderDTOQuery.getPageSize(), list);
        page.setTotalItems(mesProduceOrderService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return mesProduceOrderService.deleteById(id);
        } catch (Exception e) {
            logger.error("MesProduceOrder delete failed.", e);
            throw new RuntimeException("MesProduceOrder delete failed, Please order your code.", e);
        }
    }

    @Override
    public Long countByQuery(MesProduceOrderDTOQuery mesProduceOrderDTOQuery) {
        try {
            Query query = Query.noPage();

            if (mesProduceOrderDTOQuery.getYearOnly() != null) {
                query.put("yearOnly", mesProduceOrderDTOQuery.getYearOnly());
            }

            if (mesProduceOrderDTOQuery.getYearMonth() != null) {
                query.put("yearMonth", mesProduceOrderDTOQuery.getYearMonth());
            }

            if (mesProduceOrderDTOQuery.getBeforeStatus() != null) {
                query.put("beforeStatus", mesProduceOrderDTOQuery.getBeforeStatus());
            }

            if (mesProduceOrderDTOQuery.getAfterStatus() != null) {
                query.put("afterStatus", mesProduceOrderDTOQuery.getAfterStatus());
            }

            if (mesProduceOrderDTOQuery.getStatusIds() != null) {
                query.put("statusIds", mesProduceOrderDTOQuery.getStatusIds());
            }

            query.put("stopFlag", 0);

            return mesProduceOrderService.countByQuery(query);
        } catch (Exception e) {
            logger.error("MesProduceOrder delete failed.", e);
            throw new RuntimeException("MesProduceOrder count failed, Please order your code.", e);
        }
    }

    @Override
    public List<Map<String, String>> getMapDataByQuery(MesProduceOrderDTOQuery mesProduceOrderDTOQuery, String element) {
        try {
            Query query = Query.noPage();

            if (mesProduceOrderDTOQuery.getShowDate() != null) {
                query.put("showDate", mesProduceOrderDTOQuery.getShowDate());
            }

            if (StringUtils.isNotBlank(mesProduceOrderDTOQuery.getStartDate())) {
                query.put("startDate", mesProduceOrderDTOQuery.getStartDate());
            }

            if (StringUtils.isNotBlank(mesProduceOrderDTOQuery.getEndDate())) {
                query.put("endDate", mesProduceOrderDTOQuery.getEndDate());
            }

            if (mesProduceOrderDTOQuery.getYearMonth() != null) {
                query.put("yearMonth", mesProduceOrderDTOQuery.getYearMonth());
            }

            if (StringUtils.isNotBlank(mesProduceOrderDTOQuery.getStartYearMonth())) {
                query.put("startYearMonth", mesProduceOrderDTOQuery.getStartYearMonth());
            }

            if (StringUtils.isNotBlank(mesProduceOrderDTOQuery.getEndYearMonth())) {
                query.put("endYearMonth", mesProduceOrderDTOQuery.getEndYearMonth());
            }

            query.put("stopFlag", 0);
            query.put("deleteFlag", 0);

            return mesProduceOrderService.getMapDataByQuery(query, element);
        } catch (Exception e) {
            logger.error("MesProduceOrder select map data failed.", e);
            throw new RuntimeException("MesProduceOrder select map data failed, Please order your code.", e);
        }
    }
}